/*
 * author      : dormi330
 * date        : 2018/6/24
 * description : 订单状态机
 */
package com.deep.trade.domain.order;

public enum OrderStatusEnum {

    // 正流程
    TO_BE_AUDITED("to_be_audited", "待审核"),
    TO_BE_PAID("to_be_paid", "待支付 "),
    PAID("paid", "已支付"),
    DELIVERYING("deliverying", "发货中,如多个包裹只发了一个"),
    DELIVERYED("deliverying", "已发货"),
    FINISHED("finished", "已完成"),
    COMMENTED("commented", "已评价"),

    // 未支付而结束的流程
    CANNELLED("cancelled", "已取消"),
    PAY_TIMEOUT("pay_timeout", "支付超时"),
    AUDIT_REJECTED("audit_rejected", "已审核拒绝"),

    // 退款流程
    REFUND_APPLIED("refund_applied", "退款申请中"),
    CANCEL_REFUND("refund_cancelled", "取消退款申请"),
    REFUND_ACCEPTED("refund_accepted", "退款申请通过"),
    REFUND_REJECTED("refund_rejected", "退款申请拒绝"),
    REFUNDED("refunded", "已退款"),

    // 退货流程
    RETURN_APPLIED("return_applied", "退货申请中"),
    CANCEL_RETURN("return_cancelled", "取消退货申请"),
    RETURN_ACCEPTED("return_accepted", "退货申请通过"),
    RETURN_REJECTED("return_rejected", "退货申请拒绝"),
    RETURNED("returned", "已退货"),

    // 删除
    DELETED("deleted", "已删除");

    public String name;
    public String desc;

    OrderStatusEnum(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String toString() {
        return "tradeOrderStatus = " + this.desc;
    }

    /** 从name中 生成状态 */
    public static OrderStatusEnum fromName(String name) {
        for (OrderStatusEnum oneStatus : OrderStatusEnum.values()) {
            if (oneStatus.name.equals(name)) {
                return oneStatus;
            }
        }
        return null;
    }

    /**
     * 是否可以转换到下一个状态
     * <p>
     * 并不想为此实现一个状态机
     */
    public boolean canChangeTo(OrderStatusEnum nextStatus) {
        switch (this) {
            // 待审核 -> 待支付(审核通过)
            //       -> 审核拒绝
            //       -> 取消订单
            case TO_BE_AUDITED:
                return nextStatus == AUDIT_REJECTED
                        || nextStatus == CANNELLED
                        || nextStatus == TO_BE_PAID;

            // 待支付 -> 已支付
            //       -> 已取消
            //       -> 已超时
            case TO_BE_PAID:
                return nextStatus == PAID
                        || nextStatus == CANNELLED
                        || nextStatus == PAY_TIMEOUT;

            // 已支付 -> 发货中(至少一个发货单)
            //       -> 退款中
            //       -> 发货中
            //       -> 已经完成 不用发货就能确认收货/已经发货, 卖家未更新
            case PAID:
                return nextStatus == DELIVERYING
                        || nextStatus == REFUND_APPLIED
                        || nextStatus == DELIVERYED
                        || nextStatus == FINISHED;

            // 发货中 -> 发货中(如果多个发货单, 允许继续发货)
            //       -> 退款中
            //       -> 退货中
            case DELIVERYING:
                return nextStatus == DELIVERYING
                        || nextStatus == DELIVERYED
                        || nextStatus == FINISHED
                        || nextStatus == REFUND_APPLIED
                        || nextStatus == RETURN_APPLIED;

            case DELIVERYED:
                return nextStatus == FINISHED
                        || nextStatus == REFUND_APPLIED
                        || nextStatus == RETURN_APPLIED;

            // 已完成 -> 已评价
            //       -> 已删除
            //       -> 退货中
            //       -> 退款中
            case FINISHED:
                return nextStatus == COMMENTED
                        || nextStatus == REFUND_APPLIED
                        || nextStatus == RETURN_APPLIED
                        || nextStatus == DELETED;

            // 已评价 -> 已删除
            case COMMENTED:
                return nextStatus == DELETED;

            // 已取消、支付超时、审核失败 -> 已删除
            case CANNELLED:
            case PAY_TIMEOUT:
            case AUDIT_REJECTED:
                return nextStatus == DELETED;

            // 退款申请中 -> 审核通过
            //          -> 审核拒绝
            //          -> 取消退款
            case REFUND_APPLIED:
                return nextStatus == CANCEL_REFUND
                        || nextStatus == REFUND_ACCEPTED
                        || nextStatus == REFUND_REJECTED;

            // 退款审核通过 -> 已退款
            case REFUND_ACCEPTED:
                return nextStatus == REFUNDED;

            // 退款审核不通过 -> 删除
            case REFUND_REJECTED:
                return nextStatus == DELETED;

            // 取消退款申请 -> 已删除
            case CANCEL_REFUND:
                return nextStatus == DELETED;

            // 已退款 -> 已删除
            case REFUNDED:
                return nextStatus == DELETED;

            // 退货申请中 -> 已取消
            //           -> 已通过
            //           -> 已拒绝
            case RETURN_APPLIED:
                return nextStatus == RETURN_ACCEPTED
                        || nextStatus == RETURN_REJECTED;

            // 取消退货 -> 已删除
            case CANCEL_RETURN:
                return nextStatus == DELETED;

            // 退货通过 -> 退货完成
            case RETURN_ACCEPTED:
                return nextStatus == RETURNED;

            // 退货拒绝 -> 删除
            case RETURN_REJECTED:
                return nextStatus == DELETED;

            // 已退货 -> 退款申请通过
            case RETURNED:
                return nextStatus == REFUND_ACCEPTED;

            // deleted 状态为终态
            case DELETED:
            default:
                return false;
        }
    }
}
